﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<form id="form1">
    <div class="widget-body">
        @*<div id="wizard" class="wizard" data-target="#wizard-steps" style="border-left: none; border-top: none; border-right: none;">
        <ul class="steps">
            <li data-target="#step-1" class="active">相关信息<span class="chevron"></span></li>
            <li data-target="#step-2"><span class="step">2</span>功能权限 action="~/Treatment/AForm" method="post"<span class="chevron"></span></li>
        </ul>*@
    </div>
        <div class="step-content" id="wizard-steps" style="border-left: none; border-bottom: none; border-right: none;">
            <div class="step-pane active" id="step-1" style="margin: 10px; margin-bottom: 0px;">
                @*<div class="alert alert-danger" style="text-align: left; margin-bottom: 10px;">
                    <i class="fa fa-warning alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                    请填写相应信息！
                </div>*@
                <div class="panel panel-default">
                    @*<div class="panel-heading">
                        <h3 class="panel-title">表单信息</h3>
                    </div>*@
                    <div class="panel-bodys" style="width: 95%;padding-left:15px;">
                        <table class="form">
                            <tr>
                                <th class="formTitle"><span class="required">*</span>组织机构：</th>
                                <td class="formValue formDdlSelectorTd">
                                    <select id="OrganizeId" name="OrganizeId" class="form-control required">
                                        <option value="">==请选择==</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <th class="formTitle"><span class="required">*</span>诊疗组合名称：</th>
                                <td class="formValue">
                                    <input id="zhmc" name="zhmc" type="text" class="form-control required" placeholder="请输入诊疗组合名称" />
                                </td>
                            </tr>
                            <tr>
                                <th class="formTitle"><span class="required">*</span>诊疗组合编码：</th>
                                <td class="formValue">
                                    <input id="zhcode" name="zhcode" type="text" class="form-control" placeholder="请输入诊疗组合编码" />
                                </td>
                            </tr>
                            <tr>
                                <th class="formTitle">明细项目排序：</th>
                                <td class="formValue">
                                    <input id="ord" name="ord" type="text" class="form-control" placeholder="请输入明细项目排序" />
                                </td>
                            </tr>
                            <tr hidden>
                                <th class="formTitle">诊疗明细项目：</th>
                                <td class="formValue">
                                    <input id="zlxm" name="zlxm" type="text" class="form-control" placeholder="请输入诊疗明细项目" />
                                </td>
                            </tr>
                            <tr>
                                <th class="formTitle">诊疗明细项目名称：</th>
                                <td class="formValue">
                                    <input id="zlxmmc" name="zlxmmc" type="text" class="form-control" placeholder="请输入诊疗明细项目名称" style="width:78%;float:left" />
                                    <input type="button" id="tjmx" value="添加明细" onclick="tianjiamx()" class="btn btn-default" style="width:60px;height:20px;float:left" />
                                </td>
                            </tr>
                            <tr hidden>
                                <th class="formTitle">诊疗明细项目单价：</th>
                                <td class="formValue">
                                    <input id="price" name="price" type="text" class="form-control" placeholder="请输入诊疗明细项目单价" />
                                </td>
                            </tr>
                            <tr hidden>
                                <th class="formTitle">诊疗明细项目拼音：</th>
                                <td class="formValue">
                                    <input id="zlxmpy" name="zlxmpy" type="text" class="form-control" placeholder="请输入诊疗明细项目拼音" />
                                </td>
                            </tr>
                            <tr hidden>
                                <th class="formTitle">组合金额：</th>
                                <td class="formValue">
                                    <input id="zhje" name="zhje" type="text" class="form-control" placeholder="请输入组合金额" />
                                </td>
                            </tr>
                        </table>
                        <div id="divMXTable">
                            <table id="MXTable"></table>
                        </div>



                        <p>
                            <input type="button" id="addmx" value="保存" onclick="addMX()" class="btn btn-default" style="float:right;width:40px;height:20px;margin-top:5px;" />
                            <input type="button" id="addmx" value="删除" onclick="deleteMX()" class="btn btn-default" style="float:right;margin-right:20px;width:40px;height:20px;margin-top:5px;" />
                            @*<input type="button" id="addbc" value="保存" onclick="addBC()" class="btn btn-default" style="margin-left:400px;width:40px;height:20px;"margin-left:400px;width:40px;height:20px; />*@

                        </p>
                       
                        @*<a class="btn btn-default" style="margin-left:400px;width:40px;height:20px;" onclick="addBC()">保存</a>*@
                    </div>
                </div>
            </div>
            <div class="step-pane" id="step-2">
                <div>
                    <div id="permissionTree"></div>
                </div>
            </div>
        </div>
        @*<div class="form-button" id="wizard-actions">
            <a id="btn_finish2" class="btn btn-default" onclick="AcceptClick()">完成</a>
            <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
            <a id="btn_next" class="btn btn-default btn-next">下一步</a>
            <a id="btn_finish" class="btn btn-default" style="display: none;" onclick="AcceptClick()">完成</a>
        </div>*@
    </div>
</form>

<script>
    
    var keyValue = $.request("keyValue");
    

    $(function () {
        initControl();
        if (!!keyValue) {
            $('#zhmc').attr("readonly", "readonly");
            $('#zhcode').attr("readonly", "readonly");
            $.najax({
                url: "/Treatment/selectid",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    
                    $("#form1").formSerialize(data);

                    //var zhmc2 = $("#zhmc").val();
                    var zhcode2 = $("#zhcode").val();
                    //var ord2 = $("#ord").val();
                    //var zlxm2 = $("#zlxm").val();
                    //var zlxmmc2 = $("#zlxmmc").val();
                    //var price2 = $("#price").val();
                    //var zlxmpy2 = $("#zlxmpy").val();

                    //var datamx2 = [{ "zhmc": zhmc2, "zhcode": zhcode2, "ord": ord2, "zlxm": zlxm2, "zlxmmc": zlxmmc2, "price": price2, "zlxmpy": zlxmpy2 }];
                    //console.info(JSON.parse(data));
                   

                    //$("#MXTable").addRowData(rowid2, datamx2, "last");


                    $.ajax({
                        url: "/Treatment/Codecx",  //请求的url地址
                        dataType: "json",   //返回格式为json
                        async: true,
                        data: { zhcode: zhcode2 },  //参数值
                        type: "POST",   //请求方式
                        traditional: true,
                        success: function (str) {
                            //console.info("这是data返回值" + JSON.stringify(str));
                            for (var i = 0; i < str.length; i++) {
                                //console.info("循环数据:" + JSON.stringify(str[i]));
                                $("#MXTable").addRowData(i, str[i], "last");
                            }
                            
                            //请求成功时处理
                        },
                        error: function () {
                            //请求出错处理
                        }
                    });


                    //$.najax({
                    //    url: "/Treatment/Codecx",
                    //    dataType: "json",
                    //    data: { zhcode: zhcode2 },
                    //    traditional: true,
                    //    type: "POST",
                    //    success: function (str) {
                    //        for (var i = 0; i < str.length; i++) {
                    //            console.info("循环数据:" + JSON.stringify(str[i]));
                    //            $("#MXTable").addRowData(i, str[i], "last");
                    //        }

                    //    }
                    //});



                    $("#OrganizeId").prop("disabled", true);
                }
            });
        }
    })
    

    function initControl() {
        //组织机构下拉框
        $("#OrganizeId").bindSelect({
            url: "/Organize/GetChildTreeSelectJson",
            selectedValue: $.request('orgId'),
        });

      
        $("#permissionTree").treeview({
            height: 400,
            slimscroll: false,
            showcheck: true,
            url: "/SystemManage/SysRoleAuthorize/GetPermissionTree",
            param: { roleId: keyValue }
        });
    }

    $("#zlxmmc").click(function () {
        $("#zlxmmc").val('');
    });
    
    var sfxmmc123 = "";
    var price123 = "";

    var zlxmpy123 = "";
    var sfdl = "";
    var zlxm123 = "";
    

    $('#zlxmmc').newtouchFloatingSelector({
        width: 600,
        height: 220,
        caption: "收费项目",
        clickautotrigger: true,
        url: '/SystemManage/BaseData/SelectSfxmYp',
        ajaxparameters: function () {
            return "keyword=" + $.trim($('#zlxmmc').val()) + "&mzzybz=1&dllb=2&sfdllx=OutpatientReg";
        },
        itemdbclickhandler: function ($thistr) {
            $('#zlxmmc').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-sfxmmc'));
            $('#price').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-dj'));
            $('#zlxmpy').attr('data-ghlx', $thistr.attr('data-sfxmCode'))
                .attr('data-sfxmmc', $thistr.attr('data-sfxmmc'))
                .val($thistr.attr('data-py'));

            sfxmmc123 = $thistr.attr('data-sfxmmc');
            price123 = $thistr.attr('data-dj');
            zlxmpy123 = $thistr.attr('data-py');

            sfdl = $thistr.attr('data-sfdlmc');
            zlxm123 = $thistr.attr('data-sfxmCode');
        },
        colModel: [{ label: '编码', name: 'sfxmCode', widthratio: 15 },
        { label: '名称', name: 'sfxmmc', widthratio: 25 },
        { label: '拼音', name: 'py', widthratio: 15 },
        { label: '单位', name: 'dw', widthratio: 8 },
        { label: '单价', name: 'dj', widthratio: 10 },
        { label: '收费大类', name: 'sfdlmc', widthratio: 15 },
        ]
    });
    



    function tianjiamx() {
        console.info("收费大类" + sfdl);
        var zlxmmcnull = $("#zlxmmc").val();
        if (!zlxmmcnull) {
            $.modalAlert("名称不能为空", 'warning');
        }
        else {

        
        if (!keyValue) {
            var btcode = $("#zhcode").val();
            var btzhmc = $("#zhmc").val();
            if (!btcode || !btzhmc) {
                $.modalAlert("名称和编码为必填项！", 'warning');
                return;
            }

            //判断文本框中的编码和明细表中的编码是否一致
            var jjzhcode = $("#MXTable").getCol("zhcode", false);
            
            var jjzhcodelie = $("#zhcode").val();
            var inzhcode = $.inArray(jjzhcode, chkzhcode)

           
            var count = $("#MXTable").getGridParam("reccount");
            
            if (count > 0) {
                if (jjzhcode != jjzhcodelie) {
                    $.modalAlert("两个编码不一样", 'warning');
                    addMX();
                    $("#MXTable").jqGrid("clearGridData");
                    
                }
            }
            var zhmc = $("#zhmc").val();
            var zhcode = $("#zhcode").val();
            var ord = $("#ord").val();
            //var zlxm = $("#zlxm").val();
            var datamx = [{ "zhmc": zhmc, "zhcode": zhcode, "ord": ord, "zlxm": zlxm123, "zlxmmc": sfxmmc123, "price": price123, "zlxmpy": zlxmpy123, "sfdl": sfdl }];
            //console.info(JSON.parse(data));
            var rowid = 0;


            var zhcodetj = $("#zhcode").val();
            
            //var id = $("#MXTable").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            //var rowData = $("#MXTable").jqGrid("getRowData", id);//根据上面的id获得本行的所有数据
            //var zhcodetj2 = rowData.zhcode; //获得制定列的值 （auditStatus 为colModel的name）
            //var zlxmmccx = rowData.zlxmmc;

            //获取某列数据
            var chkzhcode = $("#MXTable").getCol("zhcode", false);
            var inzhcode = $.inArray(zhcodetj, chkzhcode);
            var chkzlxmmc = $("#MXTable").getCol("zlxmmc", false);
            var inzlxmmc = $.inArray(sfxmmc123, chkzlxmmc);

            

            if (inzhcode = -1 && inzlxmmc == -1) {
                $.ajax({
                    url: "/Treatment/Tjchaxun",  //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,
                    data: { zhcodetj: zhcodetj, sfxmmc123: sfxmmc123 },  //参数值
                    type: "POST",   //请求方式
                    traditional: true,
                    success: function (data) {
                        
                        if (data = "0") {
                            $("#MXTable").addRowData(rowid, datamx, "last");
                        }
                        else {
                            $.modalAlert("该明细已存在",'success')
                        }

                        // $("#MXTable").addRowData(rowid, data, "last");
                        //请求成功时处理
                    },
                    error: function () {
                        //请求出错处理
                    }
                });
            }
            else {
                
                $.modalAlert("该明细已存在", 'success');
            }
        }
        else {
            
            $('#zhmc').attr("readonly", "readonly");
            $('#zhcode').attr("readonly", "readonly");
            
            var count = $("#MXTable").getGridParam("reccount");
            
            if (count > 0) {
                if (jjzhcode != jjzhcodelie) {
                    $.modalAlert("两个编码不一样", 'warning');
                    addMX();
                    $("#MXTable").jqGrid("clearGridData");

                }
            }
            
            var zhmc = $("#zhmc").val();
            var zhcode = $("#zhcode").val();
            var ord = $("#ord").val();
            //var zlxm = $("#zlxm").val();
            var datamx = [{ "zhmc": zhmc, "zhcode": zhcode, "ord": ord, "zlxm": zlxm123, "zlxmmc": sfxmmc123, "price": price123, "zlxmpy": zlxmpy123, "sfdl": sfdl }];
            console.info("sfdl" + JSON.stringify( datamx));
            //console.info(JSON.parse(data));
            var rowid = 0;
            
            var zhcodetj = $("#zhcode").val();

            //var id = $("#MXTable").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            //var rowData = $("#MXTable").jqGrid("getRowData", id);//根据上面的id获得本行的所有数据
            //var zhcodetj2 = rowData.zhcode; //获得制定列的值 （auditStatus 为colModel的name）
            //var zlxmmccx = rowData.zlxmmc;

            //获取某列数据
            var chkzhcode = $("#MXTable").getCol("zhcode", false);
            var inzhcode = $.inArray(zhcodetj, chkzhcode);
            var chkzlxmmc = $("#MXTable").getCol("zlxmmc", false);
            var inzlxmmc = $.inArray(sfxmmc123, chkzlxmmc);
            
            if (inzhcode = -1 && inzlxmmc == -1) {
                $.ajax({
                    url: "/Treatment/Tjchaxun",  //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,
                    data: { zhcodetj: zhcodetj, sfxmmc123: sfxmmc123 },  //参数值
                    type: "POST",   //请求方式
                    traditional: true,
                    success: function (data2) {
                        
                        if (data2 = "0") {
                            $("#MXTable").addRowData(rowid, datamx, "last");
                        }
                        else {
                            $.modalAlert("该明细已存在",'success')
                        }

                        // $("#MXTable").addRowData(rowid, data, "last");
                        //请求成功时处理
                    },
                    error: function () {
                        //请求出错处理
                    }
                });
            }
            else {

                $.modalAlert("该明细已存在",'success')
            }
        }
        }
        
        $("#zlxmmc").val('');

    }
    
    function deleteMX() {
        var keyValue = $.request("keyValue");
        console.info("获取keyvalue值"+keyValue)
        if (!keyValue) {
            var rowKey = $("#MXTable").jqGrid('getGridParam', "selrow");
            var rowid = $("#MXTable").getGridParam("selrow");
            $("#MXTable").delRowData(rowid);
        }
        else {
            var keyValue = $("#MXTable").jqGridRowValue().zlxmmc;
            var zhcodemc = $("#zhcode").val();
            console.info("这是获取的ID" + keyValue);
            if (!!!keyValue) {
                $.modalAlert("请选中一条信息", 'warning');
                return;
            }
            $.deleteForm({
                url: "/Treatment/deletemc",
                param: { keyValue: keyValue, zhcodemc: zhcodemc },
                success: function () {
                    //var rowKey = $("#MXTable").jqGrid('getGridParam', "selrow");
                    var rowid = $("#MXTable").getGridParam("selrow");
                    $("#MXTable").delRowData(rowid);
                    $.currentWindow().$("#MXTable").trigger("reloadGrid");
                }
            })
        }
        
    }
    
    function addBC() {



        var postData = $("#form1").formSerialize();
        postData["permissionIds"] = String($("#permissionTree").getCheckedNodes());

        //$.submitForm({
        //    param: postData,
        //    url: "/Treatment/tianjia?keyValue=" + keyValue,
        //    success: function () {

        //        $.currentWindow().$("#gridList").resetSelection();
        //        $.currentWindow().$("#gridList").trigger("reloadGrid");
        //        $.modalClose();
        //    }

        //})

        


        var postData = $("#form1").formSerialize();
        postData["permissionIds"] = String($("#permissionTree").getCheckedNodes());
        
        $.submitForm({
            param: postData,
            url: "/Treatment/tianjia?keyValue=" + keyValue,
            success: function () {
                $.currentWindow().$("#gridList").resetSelection();
                $.currentWindow().$("#gridList").trigger("reloadGrid");
                $.modalClose();
            }
            
        })
        
    }



    function addMX() {
        
        var rowData = $('#MXTable').jqGrid('getRowData');

        //var needRefundData = $("#gridList").jqGrid('getRowData_AllLine', true);
        //处理少一行bug 
        var allOrgId = $('#MXTable').jqGrid('getDataIDs'); //这里获取所有行 主键id 是全的
        rowData.push($('#MXTable').jqGrid('getRowData', allOrgId[allOrgId.length - 1]));
        
        

        var dataset = JSON.stringify(rowData);

        var lastdata = $("#MXTable").jqGrid('getRowData_AllLine');
        $.najax({
            url: "/Treatment/rowdata",
            dataType: "json",
            data: { dataset: dataset },
            traditional: true,
            type: "POST",
            success: function (data) {
                $.modalAlert("保存成功", 'success');
                $.currentWindow().$("#gridList").resetSelection();
                $.currentWindow().$("#gridList").trigger("reloadGrid");
                $.modalClose();
                
            }
        });
        

    }
    
    $("#MXTable").dataGrid({
        //url: "/Treatment/Login",
        //height: $(window).height() - 128,
        //postData: { keyword: $("#txt_keyword").val(), OrganizeId: $("#OrganizeId").val() },
        colModel: [
            //{ label: "主键", name: "Id", hidden: true, key: true },
            { label: '编码', name: 'zhcode', width: 80, align: 'left', hidden: true },
            { label: '项目名称', name: 'zlxmmc', width: 110, align: 'left' },
            { label: '排序', name: 'ord', width: 40, align: 'left' },
            { label: '明细项目', name: 'zlxm', width: 84, align: 'left' },
            
            { label: '项目单价', name: 'price', width: 90, align: 'left', formatter: 'number', formatoptions: { thousandsSeparator: "", decimalPlaces: 2 } },
            { label: '项目拼音', name: 'zlxmpy', width: 100, align: 'left' },
            { label: '组合名称', name: 'zhmc', width: 80, align: 'left', hidden: true },
            //{ label: '组合金额', name: 'zhje', width: 100, align: 'left' },
            { label: '收费大类', name: 'sfdl', width: 100, align: 'left', hidden: true },
        ],
        width: "510",
        height: "150",
        pager: "#gridPager",
       // sortname: 'CreateTime desc',
        //viewrecords: true,
        rowNum: '10',
        unwritten: false,
    });
    
</script>
